package com.hq.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.sys.constat.SysConstant;
import com.hq.sys.controller.RoleController;
import com.hq.sys.eneity.*;
import com.hq.sys.mapper.MenuMapper;
import com.hq.sys.mapper.RoleMapper;
import com.hq.sys.mapper.RoleMenuMapper;
import com.hq.sys.service.RoleService;
import com.hq.sys.util.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.hq.sys.service.impl
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-22  16:35
 * @Description: TODO
 * @Version: 1.8
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleController roleController;

    @Override
    public DataGridView initRoleMenuTreeList(Integer roleid) {
        //查询所有可用的菜单
        List<Menu> allMenus = menuMapper.select(new Menu().setAvailable(SysConstant.AVAILABLE_TRUE));
        List<Menu> roleMenu = menuMapper.queryMenuDataByRoleId(SysConstant.AVAILABLE_TRUE,roleid);
          //List<RoleMenu> roleMenus = roleMenuMapper.select(new RoleMenu().setRid(roleid));
          //List<Menu> roleMenu = roleMenus.stream().map(RoleMenu::getMid).map(mid->menuMapper.selectByPrimaryKey(mid)).filter(Objects::noNull)distinct().collect(Collectors.toList());
          List<TreeNode> data = new ArrayList<>();
        for (Menu menu : allMenus) {
            String checkArr = SysConstant.CODE_ZERO+"";
            for (Menu roleMenu1 : roleMenu) {
                if (Objects.equals(menu.getId(), roleMenu1.getId())) {
                    checkArr = SysConstant.CODE_ONE+"";
                    break;
                }
            }
            Integer id = menu.getId();
            Integer pid = menu.getPid();
            String title = menu.getTitle();
            String href = menu.getHref();
            Boolean Spread;
            if (menu.getSpread()==SysConstant.SPREAD_TRUE){
                Spread = true;
            }else{
                Spread = false;
            }
            String target = menu.getTarget();
            String icon = menu.getIcon();
            List<TreeNode> children = null;
            data.add(new TreeNode(id, pid, title, href, Spread, target, icon,children, checkArr));
        }
        return new DataGridView(data);
    }


    @Override
    public Role queryMenuByMid(Integer mid) {
        return null;
    }


    @Override
    public DataGridView queryAllMenuForList(RoleVo roleVo) {
        Page<Object> page = PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
        Weekend<Role> weekend = Weekend.of(Role.class);
        if (StrUtil.isNotBlank(roleVo.getRolename())){
            weekend.weekendCriteria().andLike(Role::getRolename, "%" + roleVo.getRolename() + "%");
        }
        if (StrUtil.isNotBlank(roleVo.getRoledesc())){
            weekend.weekendCriteria().andLike(Role::getRoledesc, "%" + roleVo.getRoledesc() + "%");
        }
        if (Objects.nonNull(roleVo.getAvailable())){
            weekend.weekendCriteria().andEqualTo(Role::getAvailable, roleVo.getAvailable());
        }
        List<Role> data =roleMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public int addRole(RoleVo roleVo) {
        return roleMapper.insert(roleVo);
    }

    @Override
    public int updateRole(RoleVo roleVo) {
        return roleMapper.updateByPrimaryKey(roleVo);
    }

    @Override
    public int deleteRole(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }
    //批量删除
    @Override
    public int deleteBatchRole(Integer[] ids) {
        Arrays.asList(ids).stream().forEach(roleid->roleMapper.deleteByPrimaryKey(roleid));
        return 0;
    }
    //分配菜单
    @Override
    public int saveRoleMenu(RoleVo roleVo) {
        Integer roleid = roleVo.getRoleid();
        Integer[] ids = roleVo.getIds();
        //先删除当前角色所有的菜单
        roleMenuMapper.deleteByPrimaryKey(new RoleMenu().setRid(roleid));
        //再添加新的菜单
        Arrays.asList(ids).stream().forEach(mid->{
            roleMenuMapper.insert(new RoleMenu().setRid(roleid).setMid(mid));
        });
        return 0;
    }


}
